package com.engine.camci.travel.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.camci.travel.utils.TravelUtil;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-01-09
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaTravelWeb {
    /**
     * @Description: 网报系统推送行程报备数据
     * @Param: paramsJson
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2024/1/09
     */
    @POST
    @Path("/pushTravelWebData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String pushTravelWebData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        BaseBean baseBean = new BaseBean();
        JSONObject dataJson = new JSONObject();
        String message = "调用成功";
        try {
            String tablename = "uf_xcbb";
            baseBean.writeLog(">>>>>pushTravelWebData>>>>>paramsJson=" + paramsJson);
            String dataStr = Util.null2String(paramsJson.getString("data"));
            if (TravelUtil.isJSONArray(dataStr)) {
                JSONArray dataList = JSONArray.parseArray(dataStr);
                JSONObject mainDataJson = new JSONObject();
                mainDataJson.put("txr", "1");
                int totalcount = dataList.size();
                int succcount = 0;
                String msg = "";
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject detailJson = dataList.getJSONObject(i);
                    baseBean.writeLog(">>>>>pushTravelWebData>>>>>detailJson=" + detailJson);
                    String sta_date = Util.null2String(detailJson.getString("sta_date"));
                    String end_date = Util.null2String(detailJson.getString("end_date"));
                    String end_areas = Util.null2String(detailJson.getString("end_areas"));
                    String emp_no = Util.null2String(detailJson.getString("emp_no"));
                    String task_desc = Util.null2String(detailJson.getString("task_desc"));
                    String stru_no = Util.null2String(detailJson.getString("stru_no"));
                    String stru_name = Util.null2String(detailJson.getString("stru_name"));
                    String userid = TravelUtil.getResourceidByLoginid(emp_no);
                    if (!"".equals(userid)) {
                        int mainid = TravelUtil.insertModelData(tablename, mainDataJson);
                        baseBean.writeLog(">>>>>pushTravelWebData>>>>>插入人员mainid=" + mainid);
                        sta_date = sta_date.substring(0, 10);
                        end_date = end_date.substring(0, 10);
                        List<LocalDate> dateList = TravelUtil.splitByDateBetween(sta_date, end_date);
                        for (LocalDate localDate : dateList) {
                            JSONObject statusJson = TravelUtil.isExistResourceid(userid, tablename + "_dt1", localDate.toString());
                            baseBean.writeLog(">>>>>pushTravelWebData>>>>>statusJson=" + statusJson);
                            String status = Util.null2String(statusJson.getString("status"));
                            if ("I".equalsIgnoreCase(status)) {
                                JSONObject dateJson = new JSONObject();
                                dateJson.put("bbr", userid);
                                dateJson.put("xcrq", localDate.toString());
                                dateJson.put("sx", "7");
                                dateJson.put("qtdd", end_areas);
                                dateJson.put("tbbs", "1");
                                dateJson.put("nrdj",task_desc);
                                dateJson.put("mainid", mainid);
                                dateJson.put("stru_no", stru_no);
                                dateJson.put("stru_name", stru_name);
                                boolean flag1 = TravelUtil.insertDetailData(tablename + "_dt1", dateJson);
                                if (flag1) {
                                    succcount++;
                                }
                            }
                            int dataid = Util.getIntValue(statusJson.getString("id"));
                            if ("U".equalsIgnoreCase(status)) {
                                baseBean.writeLog(">>>>>pushTravelWebData>>>>>更新人员>>>dataid=" + dataid);
                                JSONObject dateJson = new JSONObject();
                                dateJson.put("bbr", userid);
                                dateJson.put("xcrq", localDate.toString());
                                dateJson.put("sx", "7");
                                dateJson.put("nrdj",task_desc);
                                dateJson.put("qtdd", end_areas);
                                dateJson.put("stru_no", stru_no);
                                dateJson.put("stru_name", stru_name);
                                boolean flag2 = TravelUtil.updateModelData(tablename + "_dt1", dataid, dateJson);
                                if (flag2) {
                                    succcount++;
                                }

                            }
                            if ("N".equalsIgnoreCase(status)) {
                                baseBean.writeLog(">>>>>pushTravelWebData>>>>>重复且跳过的人员>>>dataid=" + dataid);
                            }
                        }
                    } else {
                        if (!msg.contains(emp_no)) {
                            msg += emp_no + "人员不存在;";
                        }
                    }
                }
                dataJson.put("totalcount", totalcount);
                dataJson.put("succcount", succcount);
                dataJson.put("failcount", totalcount - succcount);
                dataJson.put("status", (totalcount - succcount) <= 0);
                dataJson.put("msg", msg);
            } else {
                code = "PARAMETER_FORMAT_ERROR";
                message = "JSON数组格式化错误，请检查参数:data";
            }
        } catch (Exception e) {
            e.printStackTrace();
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", dataJson);
        apidatas.put("message", message);
        baseBean.writeLog(">>>>>pushTravelWebData>>>>>apidatas>>>" + apidatas);
        return JSONObject.toJSONString(apidatas);
    }

}
